from fastapi import APIRouter, Query

from app.core.deps import CurrentUser
from app.core.i18n import t
from app.schemas.auth_schemas import (
    BindAccountRequest,
    CaptchaResponse,
    ChangePasswordRequest,
    LoginRequest,
    RegisterRequest,
    ResetPasswordRequest,
    SendCodeRequest,
    TokenResponse,
    UnbindAccountRequest,
    UserInfoResponse,
)
from app.schemas.response import Response
from app.services.admin.admin_user_service import AdminUserInfoService, AdminUserManageService
from app.services.auth_service import AuthService, CaptchaTypeIntEnum

router = APIRouter(prefix="/auth", tags=["认证"])


@router.post("/login", summary="登录", response_model=Response[TokenResponse])
async def login(login_info: LoginRequest):
    """
    用户登录
    支持多种登录方式:
    1. 密码登录
    2. 邮箱验证码登录
    3. 手机验证码登录
    """
    token = await AuthService.login(
        account=login_info.account,
        provider=login_info.provider,
        password=login_info.password,
        code=login_info.code,
        captcha_id=login_info.captcha_id,
        captcha_code=login_info.captcha_code,
    )
    return Response.success(data=token, message=t("auth.login_success"))


@router.post("/register", summary="注册", response_model=Response[TokenResponse])
async def register(register_info: RegisterRequest):
    """
    用户注册
    支持多种注册方式:
    1. 密码注册
    2. 邮箱验证码注册
    3. 手机验证码注册
    """
    token = await AuthService.register(
        account=register_info.account,
        provider=register_info.provider,
        password=register_info.password,
        code=register_info.code,
        captcha_id=register_info.captcha_id,
        captcha_code=register_info.captcha_code,
    )
    return Response.success(data=token, message=t("auth.register_success"))


@router.post("/logout", summary="登出", response_model=Response[bool])
async def logout(current_user: CurrentUser) -> Response[bool]:
    """用户登出"""
    result = await AuthService.logout(current_user.token)
    return Response.success(data=result, message=t("auth.logout_success"))


@router.post("/refresh", summary="刷新令牌", response_model=Response[TokenResponse])
async def refresh_token(refresh_token: str = Query(..., description="刷新令牌")) -> Response[TokenResponse]:
    """刷新访问令牌"""
    token = await AuthService.refresh_token(refresh_token)
    return Response.success(data=token, message=t("auth.token_refresh_success"))


@router.post("/reset-password", summary="重置密码", response_model=Response[None])
async def reset_password(reset_info: ResetPasswordRequest) -> Response[None]:
    """重置密码"""
    await AuthService.reset_password(
        account=reset_info.account,
        provider=reset_info.provider,
        new_password=reset_info.new_password,
        code=reset_info.code,
    )
    return Response.success(message=t("auth.reset_password_success"))


@router.post("/change-password", summary="修改密码", response_model=Response[None])
async def change_password(change_info: ChangePasswordRequest, current_user: CurrentUser) -> Response[None]:
    """修改密码"""
    await AuthService.change_password(
        user_id=current_user.id,
        old_password=change_info.old_password,
        new_password=change_info.new_password,
    )
    return Response.success(message=t("auth.change_password_success"))


@router.post("/bind", summary="绑定账号", response_model=Response[None])
async def bind_account(bind_info: BindAccountRequest, current_user: CurrentUser) -> Response[None]:
    """绑定账号"""
    await AuthService.bind_account(
        user_id=current_user.id,
        provider=bind_info.provider,
        identifier=bind_info.identifier,
        code=bind_info.code,
        credential=bind_info.credential,
    )
    return Response.success(message=t("auth.bind_account_success"))


@router.post("/unbind", summary="解绑账号", response_model=Response[None])
async def unbind_account(current_user: CurrentUser, unbind_info: UnbindAccountRequest) -> Response[None]:
    """解绑账号"""
    await AuthService.unbind_account(user_id=current_user.id, provider=unbind_info.provider)
    return Response.success(message=t("auth.unbind_account_success"))


@router.post("/send-code", summary="发送验证码", response_model=Response[None])
async def send_code(send_info: SendCodeRequest) -> Response[None]:
    """发送验证码"""
    if send_info.provider == "email":
        await AuthService.send_email_code(send_info.account)
    elif send_info.provider == "phone":
        await AuthService.send_sms_code(send_info.account)
    return Response.success(message=t("auth.send_code_success"))


@router.get("/captcha", summary="生成图形验证码", response_model=Response[CaptchaResponse])
async def generate_captcha(
    captcha_type: CaptchaTypeIntEnum = Query(CaptchaTypeIntEnum.GIF, description="验证码类型(gif/png)")
) -> Response[CaptchaResponse]:
    """生成图形验证码"""
    captcha_id, base64_image = await AuthService.generate_captcha(captcha_type)
    return Response.success(
        data={"captcha_id": captcha_id, "captcha_image": base64_image}, message=t("auth.generate_captcha_success")
    )


# 获取用户信息
@router.get("/user-info", tags=["用户管理"], response_model=Response[UserInfoResponse])
async def get_user_info(
    current_user: CurrentUser,
):
    """获取用户信息"""
    rest = await AdminUserInfoService.get_user_info(current_user.id)
    return Response.success(data=rest)
